<template>
  <div class="city">
    <div class="list" ref="wrapper">
      <div>
        <div class="area">
          <div class="title">当前城市</div>
          <div class="button-list">
            <div class="button-wrap">
              <button class="btn">{{this.$store.state.city}}</button>
            </div>
          </div>
        </div>
        <div class="area">
          <div class="title">热门城市</div>
          <div class="button-list">
            <div class="button-wrap" v-for="item in hotCity" :key="item.id"
            @click="handleCityClick(item.name)">
              <button class="btn">{{ item.name }}</button>
            </div>
          </div>
        </div>
        <div class="area" v-for="(item, key) in city" :key="key" :ref="key">
          <div class="title">{{ key }}</div>
          <div class="item-list">
            <div class="item"   v-for="city in item" :key="city.id"
            @click="handleCityClick(city.name)"
            >
              {{ city.name }}
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
// import { mapState } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCity: Array,
    city: Object,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.$store.commit('changeCity', city)
      this.$router.push('/')
    }
  },
  computed: {
    // ...mapState(['city'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    // 监听letter的变化
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        // 利用提供的方法自动滚动到一个元素上
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.city {
  .list {
    overflow: hidden;
    position: absolute;
    top: 146px;
    bottom: 0;
    left: 0px;
    right: 0;
    .area {
      overflow: hidden;
      .title {
        line-height: 30px;
        background-color: #ccc;
        padding-left: 10px;
        font-size: 18px;
        border-top: 1px solid #777;
        border-bottom: 1px solid #777;
      }
      .button-list {
        padding: 15px 30px 15px 15px;
        overflow: hidden;
        .button-wrap {
          float: left;
          width: 33.3%;
          padding: 5px;
          .btn {
            box-sizing: border-box;
            width: 100%;
            padding: 5px 0;
            text-align: center;
            border-radius: 7px;
            border: 1px solid #777;
            font-size: 18px;
          }
        }
      }
      .item-list {
        .item {
          width: 100%;
          padding: 7px 30px 5px 15px;
          font-size: 18px;
          border-bottom: 1px solid #ccc;
        }
      }
    }
  }
}
</style>
